/*
* $Id: STreeDrawer.cpp 133 2010-08-16 16:33:49Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "STreeDrawer.h"
#include "Visual/UserMessage.h"

CSTreeDrawer::CSTreeDrawer(IWnd* pParent)
: CDrawer(pParent), m_yPos(0), m_pSelectedItem(NULL){
	m_aItems.resize(2, NULL);
}
CSTreeDrawer::~CSTreeDrawer(){
	Clear();
}

bool CSTreeDrawer::IsValidDrawer(Drawer::typeDrawer nType){
	return nType == Drawer::stree;
}

void CSTreeDrawer::Paint(CDC* dc, CRect rect){
	CDC cdc;
	cdc.CreateCompatibleDC(dc);
	CBitmap bitmapTable;
	bitmapTable.CreateCompatibleBitmap(dc, rect.Size().cx, rect.Size().cy);
	HGDIOBJ bitmapOld = cdc.SelectObject(bitmapTable);
	CBrush brush;
	brush.CreateSolidBrush(RGB(255,255,255));
	cdc.FillRect(CRect(CPoint(0, 0), rect.Size()), &brush);
	brush.DeleteObject();

	brush.CreateSolidBrush(RGB(0,0,0));
	HGDIOBJ oldBrush = cdc.SelectObject(brush);
	int nPosY = SIZE_STREE_ITEM_Y + DIST_ITEMS + m_yPos, nPosX = rect.CenterPoint().x;
	cdc.MoveTo(CPoint(nPosX, nPosY));
	cdc.LineTo(CPoint(nPosX, nPosY + DIST_ITEMS));
	if(m_aItems[0])
		m_aItems[0]->Paint(&cdc);
	else
		cdc.Ellipse(CRect(nPosX - 5, nPosY - 10, nPosX + 5, nPosY));
	nPosY += DIST_ITEMS;
	if(m_aItems[1])
		m_aItems[1]->Paint(&cdc);
	else
		cdc.Ellipse(CRect(nPosX - 5, nPosY, nPosX + 5, nPosY + 10));
	nPosY += SIZE_STREE_ITEM_Y;
	cdc.MoveTo(CPoint(nPosX, nPosY));
	cdc.LineTo(CPoint(nPosX, nPosY + DIST_ITEMS));
	nPosY += DIST_ITEMS;
	CPen pen(PS_DOT, 1, RGB(0, 0, 0));
	HGDIOBJ oldPen = cdc.SelectObject(pen);
	cdc.MoveTo(CPoint(rect.left, nPosY));
	cdc.LineTo(CPoint(rect.right, nPosY));
	cdc.SelectObject(oldPen);
	pen.DeleteObject();
	cdc.SelectObject(oldBrush);
	brush.DeleteObject();

	for(std::vector<CSTreeItem*>::iterator it = m_aItems.begin() + 2; it != m_aItems.end(); it++)
			(*it)->Paint(&cdc);

	dc->BitBlt(rect.left, rect.top, rect.Size().cx, rect.Size().cy, &cdc, 0, 0, SRCCOPY);
	cdc.SelectObject(bitmapOld);
	bitmapTable.DeleteObject();
	cdc.DeleteDC();
}

void CSTreeDrawer::SetPos(int yPos){
	int nOffset =  yPos - m_yPos;
	m_yPos = yPos;
	for(std::vector<CSTreeItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++){
		if(!*it)
			continue;
		CPoint point = (*it)->GetRect().TopLeft();
		point.Offset(0, nOffset);
		(*it)->SetPos(point);
	}
}

void CSTreeDrawer::InsertItem(int nId, const std::tstring& strName){
	std::vector<CSTreeItem*>::iterator it = m_aItems.begin() + 1;
	if(*it)
		delete *it;
	m_aItems.erase(it);
	CSTreeItem* item = new CSTreeItem(nId, strName);
	item->SetSel(true);
	m_pSelectedItem = item;
	m_aItems.insert(m_aItems.begin() + 1, item);
}

void CSTreeDrawer::InsertParentItem(int nId, const std::tstring& strName){
	std::vector<CSTreeItem*>::iterator it = m_aItems.begin();
	if(*it)
		delete *it;
	m_aItems.erase(it);
	m_aItems.insert(m_aItems.begin(), new CSTreeItem(nId, strName));
}

void CSTreeDrawer::AddChildItem(int nId, const std::tstring& strName){
	m_aItems.push_back(new CSTreeItem(nId, strName));
}

void CSTreeDrawer::Clear(){
	for(std::vector<CSTreeItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++){
		if(*it)
			delete *it;
	}
	m_aItems.clear();
	m_aItems.resize(2, NULL);
}

int CSTreeDrawer::CalcView(CRect rect){
	CPoint pos = rect.TopLeft();
	if(m_aItems[0]){
		pos.Offset(rect.CenterPoint().x - SIZE_STREE_ITEM_X/2, DIST_ITEMS + m_yPos);
		m_aItems[0]->SetPos(pos);
	}
	pos = rect.TopLeft();
	if(m_aItems[1]){
		pos.Offset(rect.CenterPoint().x - SIZE_STREE_ITEM_X/2, SIZE_STREE_ITEM_Y + 2*DIST_ITEMS + m_yPos);
		m_aItems[1]->SetPos(pos);
	}
	int size = (int)m_aItems.size() - 2;
	int nLineNum = rect.Size().cx/(SIZE_STREE_ITEM_X + DIST_ITEMS);
	int nPosY = rect.TopLeft().x + 4*DIST_ITEMS + SIZE_STREE_ITEM_Y*2 + m_yPos;
	int nPosMassive = 2;
	while(size){
		int count = 0;
		if(size - nLineNum >= 0){
			size -= nLineNum;
			count = nLineNum;
		}else{
			count = size;
			size = 0;
		}
		int dist = rect.Size().cx/count - SIZE_STREE_ITEM_X;
		int nPosX = dist/2;
		for(int i = 0; i < count; i++, nPosMassive++){
			m_aItems[nPosMassive]->SetPos(CPoint(nPosX, nPosY));
			nPosX += SIZE_STREE_ITEM_X + dist;
		}
		nPosY += SIZE_STREE_ITEM_Y + DIST_ITEMS;
	}
	return nPosY;
}

bool CSTreeDrawer::ProcessMouse(unsigned int nButton, CPoint pos, int param){
	for(std::vector<CSTreeItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++){
		if(*it && (*it)->GetRect().PtInRect(pos)){
			SelectItem(*it);
			SendSignal(UM_BUTTONCLICK, (*it)->GetId(), 0);
			return true;
		}
	}
	return false;
}

void CSTreeDrawer::SelectItem(CSTreeItem* pSelectedItem){
	if(m_pSelectedItem)
		m_pSelectedItem->SetSel(false);
	m_pSelectedItem = pSelectedItem;
	if(m_pSelectedItem)
		m_pSelectedItem->SetSel(true);
}

CSTreeItem* CSTreeDrawer::GetSelItem(){
	return m_pSelectedItem;
}
